package com.fg.cloud.system.controller.factory;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.factory.FactoryUserEntity;
import com.fg.cloud.manager.entry.factory.vo.FactoryUserVo;
import com.fg.cloud.manager.service.factory.FactoryUserService;
import com.fg.cloud.system.controller.BaseSystemController;

/**
 * Description:厂商用户
 * @author dingjiang
 * @date 2017年10月25日下午5:16:03
 */
@Controller
@RequestMapping("/facUser")
public class FacUserController extends BaseSystemController {

	@Autowired
	private FactoryUserService factoryUserService;
	
	@RequestMapping(value = "/facUserInfo")
	public String facUserInfo(HttpServletRequest req, HttpServletResponse resp){
		return "factory/factoryUserList";
	}
	
	/**
	 * Description:分页查询所有厂商用户
	 * @param req
	 * @param resp
	 * @param page
	 * @param rows
	 * @param order
	 * @param sort
	 * @return
	 * @author dingjiang
	 * @date 2017年10月25日下午5:38:06
	 */
	@RequestMapping(value = "/selectPage", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public PageUtils<FactoryUserVo> selectPage(HttpServletRequest req, HttpServletResponse resp, 
			Integer page, Integer rows, String order, String sort, String keyword, Long facId, Integer status) {
		try {
			if ( null == page ) page = 1;
			if ( null == rows ) rows = 10;
			PageUtils<FactoryUserVo> pageinfo = new PageUtils<>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("keyword", keyword);
			condition.put("facId", facId);
			condition.put("status", status);
			factoryUserService.selectPage(pageinfo);			
			pageinfo.setRows(pageinfo.getData());
			return pageinfo;
		} catch (Exception e) {
			e.printStackTrace();
			return new PageUtils<>();
		}
	}
	
	/**
	 * Description:新增厂商用户
	 * @param req
	 * @param resp
	 * @param facUserEntity
	 * @author dingjiang
	 * @date 2017年10月31日下午3:55:45
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public void insert(HttpServletRequest req, HttpServletResponse resp, FactoryUserEntity facUserEntity) {
		try {
			//验证非空
			if ( null == facUserEntity || StringUtils.isEmpty(facUserEntity.getUserName()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "用户名不能为空");
				return ;
			}
			if ( LongUtils.isEmpty(facUserEntity.getFacId()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "厂商不能为空");
				return ;
			}
			FactoryUserVo result = factoryUserService.selectUserByUserName(facUserEntity.getUserName(), facUserEntity.getFacId());
			//用户名判重
			if ( result != null ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "用户名已存在");
				return ;
			}
			setCreateInfo(facUserEntity, req);
			setUpdateInfo(facUserEntity, req);
			boolean flag = factoryUserService.insertSelective(facUserEntity);
			doResult(resp, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, null, 
					flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:修改厂商用户
	 * @param req
	 * @param resp
	 * @param facUserEntity
	 * @author dingjiang
	 * @date 2017年10月31日下午6:08:00
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public void update(HttpServletRequest req, HttpServletResponse resp, FactoryUserEntity facUserEntity){
		try {
			//验证非空
			if ( null == facUserEntity || StringUtils.isEmpty(facUserEntity.getUserName()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "用户名不能为空");
				return ;
			}
			if ( LongUtils.isEmpty(facUserEntity.getFacId()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "厂商不能为空");
				return ;
			}
			FactoryUserVo temp = factoryUserService.selectUserByUserName(facUserEntity.getUserName(), 
					facUserEntity.getFacId(), facUserEntity.getId());
			//用户名判重
			if ( temp != null ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "用户名已存在");
				return ;
			}
			setUpdateInfo(facUserEntity, req);
			boolean flag = factoryUserService.updateByPrimaryKeySelective(facUserEntity);
			doResult(resp, flag, flag?StatusCode.SAVESUCCESS:StatusCode.SAVEERROR, null, 
					flag?ResultType.SAVE_SUCCESS:ResultType.SAVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
		}
	}
	
	/**
	 * Description:批量删除
	 * @param req
	 * @param resp
	 * @param ids
	 * @author dingjiang
	 * @date 2017年11月8日下午4:41:42
	 */
	@RequestMapping(value = "/delBy", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public void delBy(HttpServletRequest req, HttpServletResponse resp, String ids){
		try {
			if ( StringUtils.isEmpty(ids) ) {
				doResult(resp, false, StatusCode.REMOVEERROR, null, "请选择需要删除的条目！");
				return ;
			}
			boolean flag = factoryUserService.removes(LongUtils.convert(ids.split("[, ]+")));
			doResult(resp, flag, flag?StatusCode.REMOVESUCCESS:StatusCode.REMOVEERROR, null, 
					flag?ResultType.REMOVE_SUCCESS:ResultType.REMOVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
		}
	}
	
	/**
	 * Description:重置用户密码
	 * @param req
	 * @param resp
	 * @param ids
	 * @author dingjiang
	 * @date 2017年11月9日下午1:44:24
	 */
	@RequestMapping(value = "/resetPsd", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public void resetPsd(HttpServletRequest req, HttpServletResponse resp, String ids){
		try {
			if ( StringUtils.isEmpty(ids) ) {
				doResult(resp, false, StatusCode.REMOVEERROR, null, "请选择需要重置密码的用户！");
				return ;
			}
			boolean flag = factoryUserService.resetPsd(LongUtils.convert(ids.split("[, ]+")));
			doResult(resp, flag, flag?StatusCode.SAVESUCCESS:StatusCode.SAVEERROR, null, 
					flag?"密码重置成功":"密码重置失败");
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.REMOVEERROR, null, "密码重置失败");
		}
	}
	
	/**
	 * Description:厂商用户角色授权
	 * @param req
	 * @param resp
	 * @param roleId
	 * @param userId
	 * @author dingjiang
	 * @date 2017年11月8日下午5:28:47
	 */
	@RequestMapping(value = "/roleAutho", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public void roleAutho(HttpServletRequest req, HttpServletResponse resp, Long roleId, Long userId){
		try {
			if ( LongUtils.isEmpty(userId) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请选择厂商用户");
				return ;
			}
			if ( LongUtils.isEmpty(roleId) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请选择角色");
				return ;
			}
			boolean flag = factoryUserService.roleAutho(userId, roleId);
			doResult(resp, flag, flag?StatusCode.SAVESUCCESS:StatusCode.SAVEERROR, null, 
					flag?"角色授权成功":"角色授权失败");
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, "角色授权失败");
		}
	}
	
	
	
	
	
	
}
